package com.teleca.jamendo.ui.activity;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;

import android.app.Activity;
import android.gesture.GestureOverlayView;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import com.teleca.jamendo.JamendoApplication;
import com.teleca.jamendo.R;
import com.teleca.jamendo.api.JamendoGet2Api;
import com.teleca.jamendo.api.WSError;
import com.teleca.jamendo.api.impl.JamendoGet2ApiImpl;
import com.teleca.jamendo.mode.Album;
import com.teleca.jamendo.ui.BaseActivity;
import com.teleca.jamendo.ui.adapter.PopularAlbumsWeekAdapter;
import com.teleca.jamendo.ui.widget.LoadingUpView;

public class PopularAlbumsWeek extends BaseActivity implements OnItemClickListener {
	private Activity mActivity;
	private TextView mTitle;
	private GridView mGvPopularWeek;
	private PopularAlbumsWeekAdapter mAlbumsAdapter;
	private List<Album> mAlbums;
	private LoadingUpView mLoadingUpView;
	private GestureOverlayView mGestureOverlayView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.popular_albums_week);
		initVariables();
		initViews();
		setListener();
		getAlbums();
	}

	private void getAlbums() {
		new NewsTask().execute((Void) null);
	}

	@Override
	protected void onResume() {
		super.onResume();
		boolean gesturesEnabled = PreferenceManager.getDefaultSharedPreferences(this).getBoolean("gestures", true);
		mGestureOverlayView.setEnabled(gesturesEnabled);
	}

	private void initVariables() {
		mActivity = this;
		mAlbums = new ArrayList<Album>();
		mLoadingUpView = new LoadingUpView(mActivity, true);
		mAlbumsAdapter = new PopularAlbumsWeekAdapter(mActivity);
		mGestureOverlayView = (GestureOverlayView) findViewById(R.id.gestures);
		mGestureOverlayView.addOnGesturePerformedListener(JamendoApplication.getInstance().getPlayerGestureHandler());

	}

	private void initViews() {
		mTitle = (TextView) findViewById(R.id.tv_title_middle);
		mGvPopularWeek = (GridView) findViewById(R.id.gv_popular_albums_week);
		mTitle.setText(R.string.popular_albums_week);
		mGvPopularWeek.setAdapter(mAlbumsAdapter);
	}

	private void setListener() {
		mGvPopularWeek.setOnItemClickListener(this);
	}

	private class NewsTask extends AsyncTask<Void, WSError, List<Album>> {

		@Override
		public void onPreExecute() {
			super.onPreExecute();
			if (null != mLoadingUpView && !mLoadingUpView.isShowing()) {
				mLoadingUpView.showPopup(mActivity.getString(R.string.album_loading));
			}
		}

		@Override
		public List<Album> doInBackground(Void... params) {
			JamendoGet2Api server = new JamendoGet2ApiImpl();
			List<Album> albums = null;
			try {
				albums = server.getPopularAlbumsWeek();
			} catch (JSONException e) {
				e.printStackTrace();
			} catch (WSError e) {
				e.printStackTrace();
			}
			return albums;
		}

		@Override
		public void onPostExecute(List<Album> albums) {
			if (null != mLoadingUpView && mLoadingUpView.isShowing()) {
				mLoadingUpView.dismiss();
			}

			if (albums != null) {
				mAlbums.clear();
				mAlbums.addAll(albums);
				mAlbumsAdapter.setList((ArrayList<Album>) albums);
			} else {
				toast(getString(R.string.net_work_eror));
			}
			super.onPostExecute(albums);
		}

		@Override
		protected void onProgressUpdate(WSError... values) {
			Toast.makeText(mActivity, values[0].getMessage(), Toast.LENGTH_LONG).show();
			super.onProgressUpdate(values);
		}
	}

	@Override
	public void onItemClick(AdapterView<?> adapterView, View arg1, int position, long arg3) {
		Album album = (Album) adapterView.getItemAtPosition(position);
		PlayerActivity.launch(mActivity, album);
	}
}
